--#include "data\config\boss\PersonBossConfig.lua" once
--#include "data\functions\Common\CommonFunc.lua" once
--#include "data\config\GlobalLuaConfig.lua" once 

PERSONBOSS_FUBEN_SCENEID	= 0

--[[
注册一些事件
]]
function ResisterPersonBossProc()
	--print("ResisterPersonBossProc...")
	for bossIdx, bossConfig in ipairs( PersonBossConfig ) do
		SetNeedReloginFuben( bossConfig.fubenId, GlobalLuaConfig.reloginFuben.personBoss, GetElemsForReLoginPersonBoss )
								--该副本是需要掉线恢复的
		--SceneEnterDispatcher.register( bossConfig.sceneId, OnEnterPersonBoss )				--注册进入场景、副本后触发的事件
		SceneExitDispatcher.register( bossConfig.sceneId,  OnCommonSceneExit )					--注册离开场景、副本后触发的事件
		--SceneAwardDispatcher.register( bossConfig.sceneId, GetPersonBossAward )				--注册场景、副本中领奖的事件(通过通用面板)
		SceneLogoutDispatcher.register( bossConfig.sceneId, OnCommonFubenLogout )				--注册场景、副本中下线触发的事件
		--FubenTimeoutDispatcher.register( bossConfig.fubenId, OnPersonBossTimeout )			--注册场景、副本时间到后触发的事件
		--SceneDieDispatcher.register( bossConfig.sceneId, OnPersonBossActorDie )				--注册场景、副本中角色死亡触发的事件
		--SceneReliveDispatcher.register( bossConfig.sceneId, OnPersonBossActorRelive )			--注册场景、副本中角色复活触发的事件
		SceneHandExitDispatcher.register( bossConfig.sceneId,  OnCommonSceneHandExit )			--注册离开场景、副本后触发的事件
	end
end

function IsInPersonBossFuben( fubenId )
	for i, bossConfig in ipairs( PersonBossConfig ) do
		if bossConfig.fubenId == fubenId then
			return true
		end
	end
	return false
end

--[[
为副本掉线恢复，获取一些数据
]]
function GetElemsForReLoginPersonBoss( fubenPtr )
	local fubenId 		= Fuben.getFuBenId(fubenPtr)
	local fubenCfg 		= GetPersonBossCfgByFubenId( fubenId )
	local InitFubenRightPanel = InitPersonBossPanel
	local loginLog   	= "person boss login" 
	local loginLogOk	= "person boss login ok"
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--[[
for test
进入个人BOSS后触发
]]
function OnEnterPersonBoss( sysarg, sceneId, lastSceneId )
	--print("OnEnterPersonBoss, sceneId="..sceneId..", lastSceneId="..lastSceneId)
	local bossCfg = GetPersonBossCfgBySceneId( sceneId )
	if not bossCfg then
		return
	end

	SetPersonBossAwardSts( sysarg, 0 )				--设置为未领奖
	FreshPersonBossMonster( sysarg, bossCfg.monsters )
	OpenPersonBossPanel( sysarg, bossCfg )
	--SendPersonBossData( sysarg )
end

--[[
副本时间到
]]
function OnPersonBossTimeout( fubenPtr )
	local fubenId 	= Fuben.getFuBenId(fubenPtr)
	--print("OnPersonBossTimeout, fubenId="..fubenId)
	--[[
	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		ProcExitPersonBoss( player, 3 )
	end
	]]
end

function OnPersonBossActorDie( sysarg )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId 	= Fuben.getFuBenId(fubenPtr)
	--print("OnPersonBossActorDie, sceneId="..sceneId..", fubenId="..fubenId)
end

function OnPersonBossActorRelive( sysarg, reliveType )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId 	= Fuben.getFuBenId(fubenPtr)
	--print("OnPersonBossActorRelive, reliveType="..reliveType..", sceneId="..sceneId..", fubenId="..fubenId)

	if reliveType == 2 then
		local maxHp = Actor.getIntProperty(sysarg, PROP_CREATURE_MAXHP)
		local hp = maxHp*0.2
		Actor.setUIntProperty(sysarg, PROP_CREATURE_HP, hp)
	end
end

--[[
进入个人BOSS
]]
function EnterPersonBoss( sysarg, bossIdx )
	--print("EnterPersonBoss, bossIdx="..bossIdx)
	local bossCfg = PersonBossConfig[bossIdx]
	if not bossCfg then
		return
	end

	if CommonEnterScence( sysarg, bossCfg, enterLogId, enterLogDesc, PersonBossCheck, PersonBossBeforeEnter ) then
		local actorId 	= Actor.getActorId( sysarg )
		local fubenPtr 	= Actor.getFubenPrt( sysarg )
		Fuben.setOwnerId( fubenPtr, actorId )
		Fuben.setFbValue( fubenPtr, PERSONBOSS_FUBEN_SCENEID, Actor.getSceneId(sysarg) )			--副本所属的场景ID

		SetPersonBossAwardSts( sysarg, 0 )				--设置为未领奖
		FreshPersonBossMonster( sysarg, bossCfg.monsters )
		InitPersonBossPanel( sysarg, bossCfg )
		SendPersonBossData( sysarg )
		--local sceneId = Actor.getSceneId( sysarg )
		--print("EnterPersonBoss, sceneId="..sceneId..", bossCfg.sceneId="..bossCfg.sceneId)
	end
end

function InitPersonBossPanel( sysarg, bossCfg )
	local contStyle 	= 2
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_PersonBoss
	local subActivityId = bossCfg.bossIdx
	local title			= Lang.ScriptTips.PersonBossName
	OpenCommonStylePanel( sysarg, 1, activityId, subActivityId, title, bossCfg, contStyle, buttons  )
end


function PersonBossCheck( sysarg, bossCfg )
	--print("PersonBossCheck...")
	--local enterTimes = GetPersonBossDailyEnterTimes( sysarg, bossCfg.bossIdx )
	--if enterTimes >= bossCfg.dailyLimit then
	--	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021,ttFlyTip)
	--	return false
	--end
	local privBossIdx = bossCfg.bossIdx - 1
	if privBossIdx > 0 then
		local hadKill = GetPersonBossHadKill( sysarg, privBossIdx )
		--print("PersonBossCheck, privBossIdx="..privBossIdx..", hadKill="..hadKill)
		if hadKill <= 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonBoss001,ttFlyTip)
			return false
		end
	end

	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_PersonBoss, bossCfg.bossIdx ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021,ttFlyTip)
		return false
	end
	return true
end

function PersonBossBeforeEnter( sysarg, bossCfg )
	--print("PersonBossBeforeEnter...")
	--local enterTimes = GetPersonBossDailyEnterTimes( sysarg, bossCfg.bossIdx )
	--SetPersonBossDailyEnterTimes( sysarg, bossCfg.bossIdx, enterTimes+1 )
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_PersonBoss, bossCfg.bossIdx, 1 ) 				--进入次数+1
	return true
end

function GetPersonBossDailyEnterTimes( sysarg, bossIdx )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.personBoss then
		sVar.personBoss = {}
	end
	return sVar.personBoss[bossIdx] or 0
end

function SetPersonBossDailyEnterTimes( sysarg, bossIdx, newTimes )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.personBoss then
		sVar.personBoss = {}
	end
	sVar.personBoss[bossIdx] = newTimes
end

--[[
bossIdx:第N个BOSS，并不是BOSSID
]]
function GetPersonBossHadKill( sysarg, bossIdx )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.personBossHadKill then
		sVar.personBossHadKill = {}
	end
	return sVar.personBossHadKill[bossIdx] or 0
end

function SetPersonBossHadKill( sysarg, bossIdx, newFlag )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.personBossHadKill then
		sVar.personBossHadKill = {}
	end
	sVar.personBossHadKill[bossIdx] = newFlag
end

--[[
初始化右侧面板

function OpenPersonBossPanel( sysarg, bossConfig )
	local fubenPtr 		= Actor.getFubenPrt(sysarg)
	local activityType 	= 1 			--副本
	local activityId 	= enRightPanelActivity_PersonBoss
	local restTime 		= Fuben.getFbTime( fubenPtr )				--副本剩余时间
	local panelInfo 	= {}
	panelInfo.sts 		= 1  				--进行中
	panelInfo.title 	= ""				--不用显示

	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime

	panelInfo.contentTitle 	= Lang.ScriptTips.panelCont05  			--副本内容Title
	panelInfo.contents 		= {}

	local allBoss, killBoss, allMon, killMon = GetPersonBossMonsterNum( sysarg, bossConfig.monsters )
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont07, killMon, allMon)
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont08, killBoss, allBoss)
	

	--panelInfo.awardTitle 	= Lang.ScriptTips.panelCont06  			--副本奖励Title
	--panelInfo.awards 		= bossConfig.awards[1]					--副本奖励

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end
]]

--[[
右侧面板内容变化
只发送变化的部分

function ChangePersonBossPanel( sysarg, bossConfig )
	local allBoss, killBoss, allMon, killMon = GetPersonBossMonsterNum( sysarg, bossConfig.monsters )

	local panelInfo 	= {}
	panelInfo.buttons   = {}
	if killBoss >= allBoss and killMon >= allMon then
		panelInfo.sts 	= 2				--已通关（要显示2个按钮）
		--panelInfo.buttons[1]	= 2							--2-单倍领奖
		--panelInfo.buttons[2]	= 3							--3-双倍领奖
	else
		panelInfo.sts 	= 1 			--进行中
		--panelInfo.buttons[1]	= 1							--1：退出副本
	end
	panelInfo.contents 	= {}
	
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont07, killMon, allMon)
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont08, killBoss, allBoss)

	ChangeActivityRightPanel( sysarg, panelInfo )
end
]]


function FreshPersonBossMonster( sysarg, monsters )
	local fubenPtr 		= Actor.getFubenPrt(sysarg)
	local fubenHandle 	= Fuben.getFubenHandle(fubenPtr)
	FreshCommonMonsters( monsters, fubenHandle )
end

--[[个人boss被击杀
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
]]
function OnPersonBossDie(monster, sysarg, monId, lastHitKiller)
	local sceneId = Actor.getSceneId( sysarg )
	local bossCfg = GetPersonBossCfgBySceneId( sceneId )
	if not bossCfg then
		return
	end
	
	local activitySts = nil
	local contStyle 	= 2
	local buttons1 		= {1}
	local buttons2 		= {1}
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, bossCfg, buttons1, buttons2 )

	if GetPersonBossHadKill( sysarg, bossCfg.bossIdx ) <= 0 then
		SetPersonBossHadKill( sysarg, bossCfg.bossIdx, 1 )					--此BOSS已经被击杀了 
	end
end


function GetPersonBossCfgBySceneId( sceneId )
	for i, bossConfig in ipairs( PersonBossConfig ) do
		if bossConfig.sceneId == sceneId then
			return bossConfig
		end
	end
	return nil
end

function GetPersonBossCfgByFubenId( fubenId )
	for i, bossConfig in ipairs( PersonBossConfig ) do
		if bossConfig.fubenId == fubenId then
			return bossConfig
		end
	end
	return nil
end



function IsInPersonBossScene( sceneId )
	--print("IsInPersonBossScene, sceneId="..sceneId)
	for i, bossConfig in ipairs( PersonBossConfig ) do
		if bossConfig.sceneId == sceneId then
			return true
		end
	end
	return false
end

--每日数据处理
function OnPersonBossDailyProc( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.personBoss = nil
end

function GetPersonBossAwardSts( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	return dVar.personBossAward or 0
end

function SetPersonBossAwardSts( sysarg, newSts )
	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personBossAward = newTimes
end

function CheckPersonBossAward( sysarg, bNotice )
	local sceneId = Actor.getSceneId( sysarg )
	--print("CheckPersonBossAward, sceneId="..sceneId)
	local bossCfg = GetPersonBossCfgBySceneId( sceneId )
	if not bossCfg then
		return false
	end

	if not IsInPersonBossScene( sceneId ) then			--不在个人BOSS场景
		if bNotice then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0023, ttFlyTip)
		end
		return false
	end

	if GetPersonBossAwardSts( sysarg ) > 0 then			--已经领取过副本通过奖励
		if bNotice then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0025, ttFlyTip)
		end
		return false
	end

	local allBoss, killBoss, allMon, killMon = GetPersonBossMonsterNum( sysarg, bossCfg.monsters )
	if killBoss < allBoss or killMon < allMon then		--尚未通关
		if bNotice then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0024, ttFlyTip)
		end
		return false
	end

	return true
end

--[[
通过邮件发通关奖励，如果不在场景、已经领取过、怪物尚未击杀完毕，则不发
奖品走掉落流程，不直接发奖或领奖了
]]
function MailPersonBossAward( sysarg )
	--print("MailPersonBossAward...")
	if not CheckPersonBossAward( sysarg, false ) then
		return
	end

	local sceneId 		= Actor.getSceneId( sysarg )
	local bossConfig 	= GetPersonBossCfgBySceneId( sceneId )
	local actorId 		= Actor.getActorId( sysarg )
	local awards 		= bossConfig.awards[1]

	SetPersonBossAwardSts( sysarg, 1 )			--设置为已领奖
	local title 		= Lang.ScriptTips.PersonBossMailTitle
	local content 		= string.format( Lang.ScriptTips.PersonBossMailContent, Fuben.getSceneNameById( sceneId ) )

	local actorLevel  	= Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	local trasAwards 	= awards --TransAwardsByType( awards, actorLevel )			--把经验库经验转化成绝对值经验
	Awards.GiveToMailById(actorId, trasAwards, title, content, GameLog.clGetPersonBossAward, Lang.ScriptTips.PersonBossLog02 )
end

--[[
领取副本奖励
awardRate：1-单倍，2-多倍
]]
function GetPersonBossAward( sysarg, awardRate )
	--[[
	--print("GetPersonBossAward, awardRate="..awardRate)
	if not CheckPersonBossAward( sysarg, true ) then
		return
	end

	local sceneId = Actor.getSceneId( sysarg )
	local bossCfg = GetPersonBossCfgBySceneId( sceneId )
	if not bossCfg then
		return
	end

	local awards = bossCfg.awards[awardRate]
	if not awards then
		return
	end

	if not IsBagGridEnough(sysarg, awards) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	local awardConsumes = bossCfg.awardConsumes[awardRate]
	if not CheckConsumeCond( sysarg, awardConsumes, 1 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0022, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, awardConsumes, 1, GameLog.clGetPersonBossAwardFee, Lang.ScriptTips.PersonBossLog01 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0022, ttFlyTip)
		return
	end

	SetPersonBossAwardSts( sysarg, 1 )				--设置为已领奖
	GiveCommonAward(sysarg, awards, GameLog.clGetPersonBossAward, Lang.ScriptTips.PersonBossLog02)
	]]
end

--[[
下发个人BOSS数据
]]
function SendPersonBossData( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sSendPersonBossData)
	if npack then
		local num = #PersonBossConfig
		DataPack.writeChar(npack, num)
		--print("SendPersonBossData, num="..num)
		for bossIdx, bossCfg in ipairs( PersonBossConfig ) do
			--local enterTimes = GetPersonBossDailyEnterTimes( sysarg, idx )
			local enterTimes = GetActorDailyDoneTimes( sysarg, enDailyOpTimes_PersonBoss, bossIdx )
			local killGlag	 = GetPersonBossHadKill( sysarg, bossIdx )
			DataPack.writeChar(npack, bossIdx)
			DataPack.writeChar(npack, enterTimes)
			DataPack.writeChar(npack, killGlag)
			--print("SendPersonBossData, bossIdx="..bossIdx..", enterTimes="..enterTimes..", killGlag="..killGlag)
		end
		DataPack.flush(npack)
	end
end

--ResisterPersonBossProc()			--注册一些事件（暂时没有BOSS之家功能，先不注册）
